package com.itheima.reggie.common;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 自定义元数据对象处理器
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        //数据存在 metaObject的 originalObject 里
        Class<?> aClass = metaObject.getOriginalObject().getClass();
        //使用map集合存储属性名和值
        Map<String, Object> param = new HashMap<>();
        try{
            for (; aClass != Object.class; aClass = aClass.getSuperclass()) {//获取本身和父级对象
                Field[] fields = aClass.getDeclaredFields();//获取所有私有字段
                for (Field field : fields) {
                    field.setAccessible(true);
                    param.put(field.getName(), field.get(metaObject.getOriginalObject()) == null ? "" : field.get(metaObject.getOriginalObject()));
                }
            }
            System.out.println(JSONObject.toJSONString(param));

//            System.out.print(param);//打印子类和父类所有字段，注意：JsonUtil改成你自己的json解析工具
        }catch (Exception e){
            e.printStackTrace();
        }
        //如果存在该属性，那么给其赋值
        if (param.containsKey("createTime")) {
            metaObject.setValue("createTime", LocalDateTime.now());
        }
        if (param.containsKey("updateTime")) {
            metaObject.setValue("updateTime", LocalDateTime.now());
        }
        if (param.containsKey("createUser")) {
            metaObject.setValue("createUser", BaseContext.getCurrentId());
        }
        if (param.containsKey("updateUser")) {
            metaObject.setValue("updateUser", BaseContext.getCurrentId());
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //数据存在 metaObject的 originalObject 里
        Class<?> aClass = metaObject.getOriginalObject().getClass();

        Map<String, Object> param = new HashMap<>();
        try{
            for (; aClass != Object.class; aClass = aClass.getSuperclass()) {//获取本身和父级对象
                Field[] fields = aClass.getDeclaredFields();//获取所有私有字段
                for (Field field : fields) {
                    field.setAccessible(true);
                    param.put(field.getName(), field.get(metaObject.getOriginalObject()) == null ? "" : field.get(metaObject.getOriginalObject()));
                }
            }
            System.out.println(JSONObject.toJSONString(param));
        }catch (Exception e){
            e.printStackTrace();
        }
        //如果存在该属性，那么给其赋值
        if (param.containsKey("updateTime")) {
            metaObject.setValue("updateTime", LocalDateTime.now());
        }
        if (param.containsKey("updateUser")) {
            metaObject.setValue("updateUser", BaseContext.getCurrentId());
        }
    }
}